<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-gb" lang="en-gb" dir="ltr" id="minwidth" >
<head>
    <meta http-equiv="content-type" content="text/html; charset=utf-8" />
</head>
    <body>
<script type="text/javascript">
RM = {};
RM.Pages = {};
RM.Pages.User = {};
RM.Pages.User.Unit = {};

RM.Common = {};
RM.Common.GUIDateFormat = "<?php echo $this->guiDateFormat ?>";
RM.Common.MySQLDateFormat = "<?php echo $this->mysqlDateFormat ?>";
RM.Common.PHPDateFormat = "<?php echo $this->phpDateFormat ?>";
RM.Common.CalendarStartDay = <?php echo $this->calStartDay ?>;

RM.BaseURL = '<?php echo RM_Environment::getInstance()->getRouter()->_(); ?>';
RM.RootURL = '<?php echo RM_Environment::getConnector()->getRootURL(); ?>';
RM.BaseLargeImageURL = RM.RootURL + 'RM/userdata/images/system/large/';
RM.BaseSmallImageURL = RM.RootURL + 'RM/userdata/images/system/small/';
RM.BaseMenuImageURL  = RM.RootURL + 'RM/userdata/images/system/menu/';
RM.BaseUnitImageURL  = RM.RootURL + 'RM/userdata/images/media/';

<?php echo $this->_router->getAccembleURLJsCode(); ?>
</script>

<?php
// set if the js files are loaded from debug or live(compressed)...
if ((int)Zend_Registry::get('config')->get('js')->get('debug')->get('user') == 1) {
    $jsLoadFolder = '/debug/';
} else {
    $jsLoadFolder = '/';
}
?>
<script type="text/javascript" src="<?php echo RM_Environment::getConnector()->getRootURL(); ?>RM/system/js<?php echo $jsLoadFolder;?>admin/common.js"></script>
<script type="text/javascript" src="<?php echo RM_Environment::getConnector()->getRootURL(); ?>RM/system/js<?php echo $jsLoadFolder;?>admin/lists.js"></script>
<script type="text/javascript" src="<?php echo RM_Environment::getInstance()->getRouter()->_('Language', 'getconstants'); ?>"></script>

<!-- ExtJS CSS -->
<link rel="stylesheet" type="text/css" href="<?php echo RM_Environment::getConnector()->getRootURL(); ?>RM/system/js/libs/ext/resources/css/ext-all.css"/>
<!--<link rel="stylesheet" type="text/css" href="<?php echo RM_Environment::getConnector()->getRootURL(); ?>RM/system/js/libs/ext/resources/css/xtheme-blueen.css" id="theme">-->

<!-- ExtJS Base -->
<?php
$RMconfig = Zend_Registry::get('config');
if ((int)$RMconfig->get('js')->get('debug')->get('user') == 1) {
    ?>
    <script language="javascript" type='text/javascript' src='<?php echo RM_Environment::getConnector()->getRootURL(); ?>RM/system/js/libs/ext/adapter/ext/ext-base-debug.js'></script>
    <script type="text/javascript" src="<?php echo RM_Environment::getConnector()->getRootURL(); ?>RM/system/js/libs/ext/ext-all-debug.js"></script>
    <?php
} else {
    ?><script language="javascript" type='text/javascript' src='<?php echo RM_Environment::getConnector()->getRootURL(); ?>RM/system/js/libs/ext/adapter/ext/ext-base.js'></script><?php
    if ((int)$RMconfig->get('gzip')->get('extjs') == 1) {
        ?><script type="text/javascript" src="<?php echo RM_Environment::getConnector()->getRootURL(); ?>RM/system/js/libs/ext/ext-all.gz.js"></script><?php
    } else {
        ?><script type="text/javascript" src="<?php echo RM_Environment::getConnector()->getRootURL(); ?>RM/system/js/libs/ext/ext-all.js"></script><?php
    }
}
?>

<!-- ExtJS CSS -->
<link rel="stylesheet" type="text/css" href="<?php echo RM_Environment::getConnector()->getRootURL(); ?>RM/system/js/libs/ext/resources/css/ext-all.css"/>
<!--<link rel="stylesheet" type="text/css" href="<?php echo RM_Environment::getConnector()->getRootURL(); ?>RM/system/js/libs/ext/resources/css/xtheme-blueen.css" id="theme">-->

<!-- calendar -->
<link rel="stylesheet" type="text/css" href="<?php echo RM_Environment::getConnector()->getRootURL(); ?>RM/system/js/libs/ext_addons/datepickerplus/datepickerplus.css"/>
<script type="text/javascript" src="<?php echo RM_Environment::getConnector()->getRootURL(); ?>RM/system/js/libs/ext_addons/datepickerplus/ext.ux.datepickerplus.js"></script>
<script type="text/javascript" src="<?php echo RM_Environment::getConnector()->getRootURL(); ?>RM/system/js/libs/ext_addons/rm/Ext.rm.form.DateField.js"></script>
<script type="text/javascript" src="<?php echo RM_Environment::getConnector()->getRootURL(); ?>RM/system/js/libs/ext_addons/rm/Ext.rm.Calendar.js"></script>

<link rel="stylesheet" type="text/css" href="<?php echo RM_Environment::getConnector()->getRootURL(); ?>RM/userdata/css/user.css"/>
<link rel="stylesheet" type="text/css" href="<?php echo RM_Environment::getConnector()->getRootURL(); ?>RM/userdata/modules/HospitalityPrices/css/main.css" />
<?php
$config = new RM_Config;
$priceSystem = RM_Prices_Manager::getInstance()->getRealPriceSystem($this->unit);

$peopleMaximums = $priceSystem->getPeopleMaximums($this->unit->id); // returns array containing "max_occupancy"=>$max_occupancy,"max_adults"=>$max_adults,"max_children"=>$max_children,"max_infants"=>$max_infants

$dateFormat = $config->getJSDateformat();
$CalStartDay = $config->getValue('rm_config_calendar_startday');
$disabledPeriods = $this->calendardata;

$criteria = RM_Reservation_Manager::getInstance()->getCriteria();

if ($dateFormat=="d/m/Y"){
    $lng_dateFormat = "dd/mm/yyyy";
} else {
    $lng_dateFormat = "mm/dd/yyyy";
}

$qtyCriteria  = 1; // the number of units selected (there will always be 1)
$qtyAvailable = 1; // the number of units available (used for the dropdown list)

if (isset($_GET['isGroup']) && (int)$_GET['qty']>1 ){
    $qtyAvailable = (int)$_GET['qty'];
}

if (isset($criteria->quantity) && $criteria->quantity>1){
    $qtyCriteria = $criteria->quantity;
}
?>
<div class="RM_datepicker_modal_container">
    <div class="RM_datepicker_modal_header">
            <?php echo $this->_translate->_('User.DatePicker','DateSelection');?>
    </div>
    <div class="RM_dateselector">
            <div class="RM_dateselector_from">
                    <div class="RM_dateselector_label"><?php echo $this->_translate->_('StartDate');?></div>
                    <div id="RM_start_dp_calendar"></div>
            </div>
            <div class="RM_dateselector_to">
                    <div class="RM_dateselector_label"><?php echo $this->_translate->_('EndDate');?></div>
                    <div id="RM_end_dp_calendar"></div>
            </div>

            <!-- maximum people selection -->
            <div class="RM_dateselector_adults">
                <div class="RM_dateselector_label"><?php echo $this->_translate->_('User.DatePicker','Adults');?></div>
                    <select id="adults">
                        <?php
                        for ($i = 1; $i <= (int)$peopleMaximums['max_adults']; $i++) {
                            if (isset($criteria)) {
                                if ($criteria->adults==$i){
                                    // render the selected
                                    ?><option value="<?php echo $i;?>" selected><?php echo $i;?></option><?php
                                } else {
                                    ?><option value="<?php echo $i;?>"><?php echo $i;?></option><?php
                                }
                            } else {
                                ?><option value="<?php echo $i;?>"><?php echo $i;?></option><?php
                            }
                        }
                        ?>
                    </select>
            </div>
            <?php if ((int)$peopleMaximums['max_children']>0){?>
            <div class="RM_dateselector_children">
                <div class="RM_dateselector_label"><?php echo $this->_translate->_('User.DatePicker','Children');?></div>
                    <select id="children" class="RM_hospitality_feild">
                        <?php
                        for ($i = 0; $i <= (int)$peopleMaximums['max_children']; $i++) {
                            if (isset($criteria)) {
                                if ($criteria->children==$i){
                                    // render the selected
                                    ?><option value="<?php echo $i;?>" selected><?php echo $i;?></option><?php
                                } else {
                                    ?><option value="<?php echo $i;?>"><?php echo $i;?></option><?php
                                }
                            } else {
                                ?><option value="<?php echo $i;?>"><?php echo $i;?></option><?php
                            }
                        }
                        ?>
                    </select>
            </div>
            <?php }
            if ((int)$peopleMaximums['max_infants']>0){?>
            <div class="RM_dateselector_infants">
                <div class="RM_dateselector_label"><?php echo $this->_translate->_('User.DatePicker','Infants');?></div>
                    <select id="infants" class="RM_hospitality_feild">
                        <?php
                        for ($i = 0; $i <= (int)$peopleMaximums['max_infants']; $i++) {
                            if (isset($criteria)) {
                                if ($criteria->infants==$i){
                                    // render the selected
                                    ?><option value="<?php echo $i;?>" selected><?php echo $i;?></option><?php
                                } else {
                                    ?><option value="<?php echo $i;?>"><?php echo $i;?></option><?php
                                }
                            } else {
                                ?><option value="<?php echo $i;?>"><?php echo $i;?></option><?php
                            }
                        }
                        ?>
                    </select>
            </div>
            <?php }?>
            <div id="RM_dateselector_boardtype" class="RM_dateselector_boardtype">
                <div class="RM_dateselector_label"><?php echo $this->_translate->_('User.DatePicker','BoardType');?></div>
                    <select id="boardtypeselect" class="RM_hospitality_feild" style="display: none;"></select>
                    <div id="select_dates_message" class="RM_select_dates_message"><?php echo $this->_translate->_('User.DatePicker','SelectDatesMessage');?></div>
            </div>
            <!-- quantity selection -->
            <div id="RM_quantity_selection" style="display: none;">
                <div class="RM_quanitity_selection_label"><?php echo $this->_translate->_('QuantityToReserve');?></div>
                <div id="RM_quanitity_selection">
                <select id="RM_dateselector_qty" name="RM_dateselector_qty">
                <?php
                if (isset($_GET['isGroup']) && $qtyAvailable>1 ){
                    for ($i = 1; $i <= $qtyAvailable; $i++) {
                        if ($qtyCriteria == $i){
                            ?><option selected value="<?php echo $i ?>" ><?php echo $i; ?></option><?php
                        } else {
                            ?><option value="<?php echo $i ?>" ><?php echo $i; ?></option><?php
                        }
                    }
                }?>
                </select>
                </div>
            </div>
        <div id="RM_dateselector_message">
            <?php echo $this->_translate->_('User.DatePicker','HelpMsg');?>
        </div>
            <div id="RM_dateselector_button">
                <input name="" type="button" value="OK" class="button" onclick="RM_dp_Submit()" />
            </div>
    </div>

</div>

<script type="text/javascript">

var RM_dp_viewFormat = '<?php echo $dateFormat; ?>';
var RM_dp_viewRenderFormat = '<?php echo $lng_dateFormat; ?>';
var RM_dp_submitFormat = 'Y-m-d';

function RM_dp_Submit(){

    //check we are not exceeding our unit people limits.
    var adultsValue = parseInt(document.getElementById('adults').value);
    if (document.getElementById('children')!==null) {var childrenValue = parseInt(document.getElementById('children').value);}
    if (document.getElementById('infants')!==null) {var infantsValue = parseInt(document.getElementById('infants').value);}
    var maxOccupancy = <?php echo $peopleMaximums['max_occupancy'];?>;
    var quantity = document.getElementById('RM_dateselector_qty').value;

    if ((adultsValue+childrenValue+infantsValue)>maxOccupancy){
        var message = "<?php echo $this->_translate->_('User.DatePicker','ExceededMaxOccupancy');?>";
        document.getElementById('RM_dateselector_message').innerHTML = message.replace(/\[XX]/g, maxOccupancy);
        return;
    }

    document.getElementById('RM_dateselector_button').style.display = "none";
    document.getElementById('RM_dateselector_message').innerHTML = "<?php echo $this->_translate->_('User.DatePicker','SavingDates');?>";

    var startDate = Date.parseDate(Ext.getCmp('rm_date_startdate_time').value, RM_dp_viewFormat);
    var endDate = Date.parseDate(Ext.getCmp('rm_date_enddate_time').value, RM_dp_viewFormat);
    var boardtype = document.getElementById('boardtypeselect').value;
    var otherInfoArray = {k: "board_type", value: boardtype};

    var conn = new Ext.data.Connection();
    var request = {
        url: RM.Common.AssembleURL({
            controller : 'Reservations',
            action: 'setdatecriteriajson'
        }),
        params: {
            unit_id : <?php echo $this->unit->getId();?>,
            startdate: startDate.format(RM_dp_submitFormat),
            enddate: endDate.format(RM_dp_submitFormat),
            adults: adultsValue,
            children: childrenValue,
            infants: infantsValue,
            otherinfo: Ext.util.JSON.encode(otherInfoArray),
            qty: quantity
        },
        method: 'POST',
        success: function(responseObject) {
            var json = Ext.util.JSON.decode(responseObject.responseText)
            if (json.success===true){
                // if the config is set to go to cart on selection
                if ("1" === "<?php echo $config->getValue('rm_config_gotocartsfterselection_enabled'); ?>"){
                    // cart url
                    var cartURL = RM.Common.AssembleURL({
                        controller : 'Reservations',
                        action: 'cart'
                    });
                    window.parent.location = cartURL;
                } else {
                    // reload the parent window
                    window.parent.location.reload();
                }
            } else {
                document.getElementById('RM_dateselector_message').innerHTML = json.error;
                document.getElementById('RM_dateselector_button').style.display = "block";
            }
        },
        failure: function(){
            document.getElementById('RM_dateselector_message').innerHTML = "<?php echo $this->_translate->_('User.DatePicker','AJAXFailure');?>";
        }
    };
    conn.request(request);
};

/**
* return the hospitality accommadation types available for this unit (full, half, b&b, all inc)
*/
function getBoardTypes(startDate, endDate){

    var bb = "<?php echo $this->_translate->_('User.DatePicker','bb');?>";
    var sc = "<?php echo $this->_translate->_('User.DatePicker','sc');?>";
    var hb = "<?php echo $this->_translate->_('User.DatePicker','hb');?>";
    var fb = "<?php echo $this->_translate->_('User.DatePicker','fb');?>";
    var ai = "<?php echo $this->_translate->_('User.DatePicker','ai');?>";


    var startDate = Date.parseDate(startDate, RM_dp_viewFormat);
    var endDate = Date.parseDate(endDate, RM_dp_viewFormat);

    var conn = new Ext.data.Connection();
    var request = {
        url: RM.Common.AssembleURL({
            controller : 'HospitalityPrices',
            action: 'getboardtypeJson'
        }),
        params: {
            unit_id : <?php echo $this->unit->getId();?>,
            startdate: startDate.format(RM_dp_submitFormat),
            enddate: endDate.format(RM_dp_submitFormat)
        },
        method: 'POST',
        success: function(responseObject) {
            var json = Ext.util.JSON.decode(responseObject.responseText);
            if (json.length>0){

                var boardtypeselect = document.getElementById("boardtypeselect");

                // clear old values
                boardtypeselect.options.length = 0;

                // add new values
                for (var m = 0; m < json.length; m++){
                    boardtypeselect.options[boardtypeselect.options.length] = new Option(eval(json[m].board_type), json[m].board_type);
                }

                // show the select box
                document.getElementById("boardtypeselect").style.display = "block";
                document.getElementById("select_dates_message").style.display = "none";
            }
        },
        failure: function(){
            document.getElementById('RM_dateselector_message').innerHTML = "<?php echo $this->_translate->_('User.DatePicker','AJAXFailure');?>";
        }
    };
    conn.request(request);
}

Ext.onReady(function(){

    var disabledData = Ext.util.JSON.decode('<?php echo $disabledPeriods;?>');

    <?php
    if (is_object($criteria) && $criteria->start_datetime) {
        ?>
        var startDate = RM.Common.ConvertToDate('<?php echo date("Y-m-d", strtotime($criteria->start_datetime));?>');
        <?php
    } else {
        ?>
        var startDate = new Date();
        <?php
    }
    if (is_object($criteria) && $criteria->end_datetime) {
        ?>
        var endDate = RM.Common.ConvertToDate('<?php echo date("Y-m-d", strtotime($criteria->end_datetime));?>');
        <?php
    } else {
        ?>
        var endDate = new Date();
        <?php
    }
    ?>
    getBoardTypes(startDate.format(RM_dp_viewFormat), endDate.format(RM_dp_viewFormat));


    var startDatePicker = new Ext.rm.form.DateField({
            id: "rm_date_startdate_time",
            startDay: <?php echo $CalStartDay;?>,
            disabledDatePeriods: disabledData,
            allowBlank: false,
            readOnly: false,
            showActiveDate: true,
            disabledLetter: "X",
            value: startDate,
            minDate: new Date(),
            useQuickTips: false,
            markNationalHolidays: false,
            monthNames: RM.Translate.Common.JSON.MonthNames,
            dayNames: RM.Translate.Common.JSON.DayLetters,
            showWeekNumber: false,
            width: 150,
            format: "<?php echo $dateFormat;?>",
            listeners: {
                select: function(dp,date) {
                    Ext.getCmp('rm_date_enddate_time').setValue(date);

                    var endDate = Ext.getCmp('rm_date_enddate_time').value;
                    getBoardTypes(date.format(RM_dp_viewFormat), endDate);
                }
            }
    });

    startDatePicker.render('RM_start_dp_calendar');

    var endDatePicker = new Ext.rm.form.DateField({
            id: "rm_date_enddate_time",
            startDay: <?php echo $CalStartDay;?>,
            disabledDatePeriods: disabledData,
            allowBlank: false,
            readOnly: false,
            showActiveDate: true,
            disabledLetter: "X",
            value: endDate,
            minDate: new Date(),
            useQuickTips: false,
            markNationalHolidays: false,
            monthNames: RM.Translate.Common.JSON.MonthNames,
            dayNames: RM.Translate.Common.JSON.DayLetters,
            showWeekNumber: false,
            width: 150,
            format: "<?php echo $dateFormat;?>",
            listeners: {
                select: function(dp,date) {
                    var startDate = Ext.getCmp('rm_date_startdate_time').value;
                    getBoardTypes(startDate, date.format(RM_dp_viewFormat));
                }
            }
    });

    endDatePicker.render('RM_end_dp_calendar');

    // invoke the board-type selection if there this is in the initialisation settings
    var maxpeople;
    var criteriaStartDate;

    <?php
    if (isset($peopleMaximums['saved_board'])){
        echo "maxpeople = ".$peopleMaximums['saved_board'].";";
    } else {
        echo "maxpeople = false;";
    }


    if (isset($criteria->start_datetime)){
        echo 'criteriaStartDate = "'.$criteria->start_datetime.'";';
    } else {
        echo 'criteriaStartDate = "";';
    }
    ;?>

    if (maxpeople || criteriaStartDate !== ""){
        var startDate = Ext.getCmp('rm_date_startdate_time').value;
        var endDate = Ext.getCmp('rm_date_enddate_time').value;
        getBoardTypes(startDate, endDate);
    }

    // if it's a group show the qty feild
    if ("<?php if (isset($_GET['isGroup'])){
            echo $_GET['isGroup'];
        } else {
            echo "0";
        }?>" === "1"){
        if (parseInt("<?php echo $qtyAvailable;?>",10) >1){
            document.getElementById("RM_quantity_selection").style.display = "block";
        }
    }

});
</script>
        </body>
</html>